//
//  VisitorView.swift
//  microblog
//
//  Created by cy on 2017/8/30.
//  Copyright © 2017年 Aisino. All rights reserved.
//

import UIKit


@IBDesignable  // 会将绘制好的图形显示在storyboard上
class VisitorView: UIView {
    
    /// 访客视图的信息字典 [imageName / message]
    var visitorInfo: [String: String]? {
        didSet {
            // 取字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    
                    return
            }
            
            if imageName == "" {  // 首页使用默认布局
                startAnimation()
                return
            }
            else { // 其他控制器的访客视图不需要显示小房子/遮罩视图
                houseIconView.isHidden = true
                maskIconView.isHidden = true
            }
            
            tipLabel.text = message
            iconView.image = UIImage(named: imageName)?.image(tintColor: UIColor.orange)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 旋转图标动画（首页）
    private func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        // 动画完成不删除，如果 iconView 被释放，动画会一起销毁！
        // 在设置连续播放的动画非常有用！
        anim.isRemovedOnCompletion = false
        
        // 将动画添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 私有控件
    /// 懒加载属性只有调用 UIKit 控件的指定构造函数，其他都需要使用类型
    /// 图像视图
    fileprivate lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩图像 - 不要使用 maskView
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    fileprivate lazy var houseIconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    fileprivate lazy var tipLabel: UILabel = UILabel.cz_label(
        withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
    
    /// 注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    
    /// 登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
}


// MARK: - 设置界面
extension VisitorView {

    func setupUI() {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        // 1、 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipLabel.textAlignment = .center
        iconView.image = iconView.image?.image(tintColor: UIColor.orange, blendMode: .overlay)
        houseIconView.image = houseIconView.image?.image(tintColor: UIColor.purple, blendMode: .overlay)

        
        // 2、snapkit 自动布局
        let margin = 20.0
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-64)
        }
        
        houseIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom)
            make.width.equalTo(236)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(margin)
            make.width.equalTo(100)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.width.equalTo(registerButton)
        }
        
        maskIconView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
    }
}
